﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ODM_Code.Forms
{
    public partial class FlammapForm : Form
    {
        private Code.FlammapCode flc;
        private string dbfPath;
        private string inputPath;
        private string alternativePath;
        private Dictionary<int, Code.FlammapCode.firedata> scenarios;
        private bool dataChanged = false;

        public FlammapForm()
        {
            InitializeComponent();

            dbfPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea");
            inputPath = dbfPath + "\\input";
            alternativePath = inputPath + "\\" + ConfigSettings.ReadSetting("currentAlternative");
            flc = new Code.FlammapCode(dbfPath, inputPath, alternativePath);
        }

        private void FlammapForm_Load(object sender, EventArgs e)
        {
            this.UpdateHelper(true);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selIndex = listBox1.SelectedIndex;

            Code.FlammapCode.firedata fire;
            if (scenarios.TryGetValue(selIndex + 1, out fire) == true)
            {
                tbScenarioId.Text = fire.problem_id;
                tbScenarioWeight.Text = fire.weight;
                tbDescription.Text = fire.description;
                tbFMSPath.Text = fire.fmsPath;
                tbIgnitionPath.Text = fire.igniPath;
                tbFoliarMoisture.Text = fire.foilmoist;
                tbWindDirection.Text = fire.winddir;
                tbWindSpeed.Text = fire.windspd;
            }
            else
            {
                tbScenarioId.Text = "";
                tbScenarioWeight.Text = "";
                tbDescription.Text = "";
                tbFMSPath.Text = "";
                tbIgnitionPath.Text = "";
                tbFoliarMoisture.Text = "";
                tbWindDirection.Text = "";
                tbWindSpeed.Text = "";
            }
        }

        private void rbWindConstant_CheckedChanged(object sender, EventArgs e)
        {
            if (rbWindConstant.Checked)
            {
                tbWindDirection.Enabled = true;
                tbWindSpeed.Enabled = true;
                tbDirectionGrid.Enabled = false;
                tbSpeedGrid.Enabled = false;
                butLoadDirectionGrid.Enabled = false;
                butLoadSpeedGrid.Enabled = false;
            }
        }

        private void rbWindGridded_CheckedChanged(object sender, EventArgs e)
        {
            if (rbWindGridded.Checked)
            {
                tbWindDirection.Enabled = false;
                tbWindSpeed.Enabled = false;
                tbDirectionGrid.Enabled = true;
                tbSpeedGrid.Enabled = true;
                butLoadDirectionGrid.Enabled = true;
                butLoadSpeedGrid.Enabled = true;
            }
        }

        private void butNew_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("New Scenario");
            listBox1.SelectedItem = "New Scenario";
        }

        private void butModify_Click(object sender, EventArgs e)
        {
            bool goodInput = this.CheckInputs();
            if (goodInput == false)
            {
                MessageBox.Show("You are missing one or more required fields. Please check your entries.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int selIndex = listBox1.SelectedIndex + 1;
            Code.FlammapCode.firedata fire;
            if (scenarios.TryGetValue(selIndex, out fire) == true)
            {
                fire.problem_id = tbScenarioId.Text;
                fire.weight = tbScenarioWeight.Text;
                fire.description = tbDescription.Text;
                fire.fmsPath = tbFMSPath.Text;
                fire.igniPath = tbIgnitionPath.Text;
                fire.foilmoist = tbFoliarMoisture.Text;

                if (rbWindConstant.Checked)
                {
                    fire.winddir = tbWindDirection.Text;
                    fire.windspd = tbWindSpeed.Text;
                }
                if (rbWindGridded.Checked)
                {
                    fire.gridWindDirPath = tbDirectionGrid.Text;
                    fire.gridWindSpeedPath = tbSpeedGrid.Text;
                }

                int fireNum = selIndex;
                flc.UpdateFireScenario(fireNum, fire);
                flc.WriteIniFile(fireNum, fire.fmsPath, fire.igniPath, fire.description);
                flc.WriteInputFile(fireNum);
                flc.WriteNewScenarios();
            }
            else
            {
                fire = new Code.FlammapCode.firedata();
                fire.problem_id = tbScenarioId.Text;
                fire.weight = tbScenarioWeight.Text;
                fire.description = tbDescription.Text;
                fire.fmsPath = tbFMSPath.Text;
                fire.igniPath = tbIgnitionPath.Text;
                fire.foilmoist = tbFoliarMoisture.Text;

                if (rbWindConstant.Checked)
                {
                    fire.winddir = tbWindDirection.Text;
                    fire.windspd = tbWindSpeed.Text;
                }
                if (rbWindGridded.Checked)
                {
                    fire.gridWindDirPath = tbDirectionGrid.Text;
                    fire.gridWindSpeedPath = tbSpeedGrid.Text;
                }

                int newFireNum = flc.AddNewFireScenario(fire);
                flc.WriteIniFile(newFireNum, fire.fmsPath, fire.igniPath, fire.description);
                flc.WriteInputFile(newFireNum);
                flc.WriteNewScenarios();
            }
            dataChanged = true;
            this.UpdateHelper(false);
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            int selIndex = listBox1.SelectedIndex + 1;
            Code.FlammapCode.firedata fire;
            if (scenarios.TryGetValue(selIndex, out fire) == true)
            {
                listBox1.Items.Remove(selIndex - 1);
                scenarios = flc.RemoveFireScenario(selIndex);
                flc.WriteNewScenarios();
                this.UpdateHelper(false);
            }
        }

        private void butDone_Click(object sender, EventArgs e)
        {
            if (dataChanged == true)
            {
                this.Close();
            }
            else
            {
                this.Close();
            }
        }

        private void UpdateHelper(bool firstRun)
        {
            listBox1.Items.Clear();

            if (firstRun)
            {
                scenarios = flc.ParseScenarios();
            }

            int firecount = flc.CountScenarios();

            for (int i = 1; i <= firecount; i++)
            {
                if (firstRun)
                {
                    flc.ParseIniFile(i);
                    flc.ParseFMS(i);
                }
                string scenName = scenarios[i].problem_id;
                listBox1.Items.Add(scenName);
            }

            if (firstRun)
            {
                tbWindDirection.Enabled = true;
                tbWindSpeed.Enabled = true;
                tbDirectionGrid.Enabled = false;
                tbSpeedGrid.Enabled = false;
                butLoadDirectionGrid.Enabled = false;
                butLoadSpeedGrid.Enabled = false;
            }
        }

        private bool CheckInputs()
        {
            bool good = true;
            TextBox[] tbs = {
                                tbScenarioId,
                                tbScenarioWeight,
                                tbDescription,
                                tbFMSPath,
                                tbIgnitionPath,
                                tbFoliarMoisture
                            };

            foreach (TextBox tb in tbs)
            {
                if (tb.Text.Equals(""))
                {
                    good = false;
                }
            }

            return good;
        }

        private void butLoadFMS_Click(object sender, EventArgs e)
        {
            ofd.InitialDirectory = alternativePath;
            ofd.Filter = "FMS Files|*.fms|All Files|*.*";
            ofd.Title = "Select a fuel moisture file";
            ofd.ShowDialog();
            tbFMSPath.Text = ofd.FileName;
        }

        private void butLoadIgnition_Click(object sender, EventArgs e)
        {
            ofd.InitialDirectory = alternativePath;
            ofd.Filter = "Shapefiles|*.shp";
            ofd.Title = "Select an ignition shapefile";
            ofd.ShowDialog();
            tbIgnitionPath.Text = ofd.FileName;
        }

    }
}
